package user.zc.file;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;

public class ResponseFile implements OutputResource {

    private HttpServletResponse resp;
    private String fileName;
    public ResponseFile(HttpServletResponse resp,String fileName){
        this.resp = resp;
        this.fileName = fileName;
    }
    @Override
    public void write(XSSFWorkbook xssfWorkbook) {
        try(OutputStream out  = resp.getOutputStream()){
            String excelName = URLEncoder.encode(fileName, "utf-8");
            resp.addHeader("content-type", "application/shlnd.ms-excel;charset=utf-8");
            resp.addHeader("content-disposition", "attachment; filename=" + excelName + ".xlsx");
            xssfWorkbook.write(out);
            out.flush();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
